import _defineProperty from '@babel/runtime/helpers/esm/defineProperty';
import _objectSpread from '@babel/runtime/helpers/esm/objectSpread2';
import _objectWithoutProperties from '@babel/runtime/helpers/esm/objectWithoutProperties';
import _slicedToArray from '@babel/runtime/helpers/esm/slicedToArray';
import { throttle } from '@szzj/utils';
import { Button, Drawer } from 'antd';
import 'antd/es/button/style/index.js';
import 'antd/es/drawer/style/index.js';
import classnames from 'classnames';
import React, { useCallback, useEffect, useMemo, useRef } from 'react';
import Form from '../../Form';
import { ClsPrefix } from '../../consts';
import './index.css';
var _excluded = [
  'form',
  'dataSource',
  'visible',
  'transform',
  'format',
  'okText',
  'cancelText',
  'onOk',
  'onCancel',
  'children',
  'formProps',
  'className',
  'throttleWait',
];
/**
 * 抽屉表单
 * @param param0
 * @returns
 */
var DrawerForm = function DrawerForm(_ref) {
  var _classnames;
  var formProp = _ref.form,
    dataSource = _ref.dataSource,
    visible = _ref.visible,
    transform = _ref.transform,
    format = _ref.format,
    okText = _ref.okText,
    cancelText = _ref.cancelText,
    onOk = _ref.onOk,
    onCancel = _ref.onCancel,
    children = _ref.children,
    formProps = _ref.formProps,
    classNameProp = _ref.className,
    throttleWait = _ref.throttleWait,
    rest = _objectWithoutProperties(_ref, _excluded);
  var _Form$useForm = Form.useForm(),
    _Form$useForm2 = _slicedToArray(_Form$useForm, 1),
    innerForm = _Form$useForm2[0];
  var form = useMemo(
    function () {
      return formProp || innerForm;
    },
    [formProp, innerForm],
  );
  var throttledSubmitRef = useRef(null);
  var submit = useCallback(
    function () {
      form
        .validateFields()
        .then(function (vals) {
          var _onOk;
          var result = transform ? transform(vals) : vals;
          onOk === null || onOk === void 0
            ? void 0
            : (_onOk = onOk(result)) === null || _onOk === void 0
            ? void 0
            : _onOk
                .then(function (res) {
                  var _throttledSubmitRef$c;
                  if (res) onCancel === null || onCancel === void 0 ? void 0 : onCancel();
                  else
                    (_throttledSubmitRef$c = throttledSubmitRef.current) === null ||
                    _throttledSubmitRef$c === void 0
                      ? void 0
                      : _throttledSubmitRef$c.cancel();
                })
                .catch(function (error) {
                  var _throttledSubmitRef$c2;
                  if (process.env.NODE_ENV === 'development') console.warn(error);
                  (_throttledSubmitRef$c2 = throttledSubmitRef.current) === null ||
                  _throttledSubmitRef$c2 === void 0
                    ? void 0
                    : _throttledSubmitRef$c2.cancel();
                });
        })
        .catch(function (err) {
          if (process.env.NODE_ENV === 'development') console.warn(err);
        });
    },
    [form, onOk, onCancel],
  );
  var throttledSubmit = useMemo(
    function () {
      var throttled = throttle(submit, throttleWait, {
        leading: true,
      });
      throttledSubmitRef.current = throttled;
      return throttled;
    },
    [submit, throttleWait],
  );
  var handleOk = useCallback(
    function () {
      if (throttleWait) return throttledSubmit();
      return submit();
    },
    [throttledSubmit, submit, throttleWait],
  );
  var handleClose = useCallback(
    function () {
      onCancel === null || onCancel === void 0 ? void 0 : onCancel();
    },
    [onCancel],
  );
  useEffect(
    function () {
      if (visible && dataSource) {
        var values = format ? format(dataSource) : dataSource;
        form.setFieldsValue(values);
      } else if (!visible) {
        form.resetFields();
      }
    },
    [visible, dataSource, form],
  );
  return /*#__PURE__*/ React.createElement(
    Drawer,
    _objectSpread(
      _objectSpread(
        {
          open: visible,
          onClose: handleClose,
          maskClosable: false,
          width: 600,
          footer: /*#__PURE__*/ React.createElement(
            'div',
            {
              style: {
                float: 'right',
              },
            },
            /*#__PURE__*/ React.createElement(
              Button,
              {
                onClick: handleClose,
              },
              cancelText || '取消',
            ),
            /*#__PURE__*/ React.createElement(
              Button,
              {
                type: 'primary',
                onClick: handleOk,
                style: {
                  marginLeft: 10,
                },
              },
              okText || '确认',
            ),
          ),
        },
        rest,
      ),
      {},
      {
        className: classnames(
          ((_classnames = {}),
          _defineProperty(_classnames, ''.concat(ClsPrefix, '-drawer-form'), true),
          _defineProperty(_classnames, classNameProp, !!classNameProp),
          _classnames),
        ),
        forceRender: true,
      },
    ),
    /*#__PURE__*/ React.createElement(
      Form,
      _objectSpread(
        {
          form: form,
          layout: 'vertical',
        },
        formProps,
      ),
      children,
    ),
  );
};
export default DrawerForm;
